package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Cart;
import csu.web.mypetstore.domain.LineItem;
import csu.web.mypetstore.domain.Order;
import csu.web.mypetstore.service.CartService;
import csu.web.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "MyOrder",urlPatterns = {"/MyOrder"})
public class MyOrderServlet extends HttpServlet {
    private static final String MY_ORDER = "/WEB-INF/jsp/order/myOrder.jsp";
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从数据库拿到order
        OrderService orderService = new OrderService();
        HttpSession session = req.getSession();
        Account account = (Account) session.getAttribute("loginAccount");
        List<Order> orders;
        orders = orderService.getOrdersByUsername(account.getUsername());
        //把orders信息存入session
        session.setAttribute("orders",orders);
        //session存一个order数量用来判断是不是空的
        session.setAttribute("orderSize",orders.size());

        session.setAttribute("action","checkOrders");

        req.getRequestDispatcher(MY_ORDER).forward(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
